package com.elmcode.server.listener;

import com.elmcode.server.config.RabbitMQConfig;
import com.elmcode.server.entity.BookEntity;
import com.elmcode.server.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@RabbitListener(queues = RabbitMQConfig.ORDER_RELEASE_ORDER_QUEUE)
@Service
@Slf4j
public class OrderCloseListener {

    @Autowired
    OrderService orderService;

    @RabbitHandler
    public void listener(BookEntity order, Channel channel, Message message) throws IOException {
         log.info("订单长时间未支付，即将修改订单状态-->已取消");
         try {
             orderService.closeOrder(order);
         }finally {
             channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
         }
    }

}
